Skip to content

vulkan: 8192 attention keys and tiled BF16 prefill (from #3417, #3431) - #6462

Open
ashhart wants to merge 2 commits into
mainfrom
lead/vulkan-sdpa-8192-tile
Open

ashhart wants to merge 2 commits into
mainfrom
lead/vulkan-sdpa-8192-tile

Conversation

@ashhart

@ashhart ashhart commented Dec 5, 2026

Copy link
Copy Markdown
Owner

Vulkan SDPA: 8192 keys and tiled BF16 prefill

This carries the work from #3417 and #3431 by @joshuaswarren onto main's own Vulkan attention shader. Thank you,
@joshuaswarren, for the capacity design, the prefill tiling, the causal guard and the served-shape regression. Both
commits credit you as co-author.

What it does

Commit 1, from #3417: 8192 visible keys.

  • The native Vulkan SDPA limit rises from 7168 to 8192 keys. 8193 is refused, never clamped.
  • Up to 7168 keys, scores stay in shared memory as today.
  • Above 7168, a GLOBAL_SCORES specialization of sdpa_decode_native.comp stores F32 scores in global memory.
    Each row and head gets its own region. A request above 7168 without that score buffer fails before dispatch.
  • The arena is 16 MiB for capacity 1..7168 and 32 MiB for capacity 7169..8192.
  • Score arithmetic, barriers and rounding are unchanged. Only where the scores are stored changes.

Commit 2, from #3431: tiled BF16 prefill.

  • BF16 query chunks of 2..64 rows use a new three-stage tile (sdpa_prefill_tile.comp): scores, softmax, PV.
    Four rows and four GQA query heads share each K and V tile load.
  • All FP32 queries, one-row decode and chunks over 64 rows keep the existing fused route.
  • The tile keeps main's arithmetic: scale * Q before an ascending dot, the 256-lane max and sum trees, 16-key PV
    partials, one divide, then RNE to BF16.
  • Each row's products use that row's own causal bound. A future NaN or Inf value never reaches an earlier row.
  • Tile workspace follows the admitted capacity: 14 MiB + 4 KiB at 7168 keys and 16 MiB + 4 KiB at 8192.
  • The attention contract adds 16 served-shape checks: rows 64/64/64/62 at prefixes 0/64/128/192, with BF16 and
    native FP32 queries, finite values and future NaN/Inf. Three planted shaders must fail the byte check.

No tolerance, no rounding change, no precision option. No other backend is touched.

Checks run

Host only. We have no shader compiler and no Vulkan headers on the build host for this change.

  • zig fmt --check --ast-check on all 16 changed Zig files: pass.
  • stage3-host-test with -Dvulkan=true: the served-shape tests run and pass (28 tests on main, 31 here). A planted
    error in the served table makes them fail. The parts that need vulkan/vulkan_core.h did not build.
  • tools/pr_check.py --base main: 0 errors. Lean 88 vs 88, no new import-cycle or cross-family edges.
  • Every file is at most 600 lines (largest 592). No comment is wider than 120 columns.

Not run: the shader builds, every GPU step, the host tests that need the Vulkan headers (attention plans at
7169/8192/8193, score workspace, prefill routing, arena sizing, request and CLI bounds), and any token, logit or
speed comparison.

Device check we ask for

@joshuaswarren, could you run this on your Vulkan GPU? We have no non-Apple Vulkan GPU to run it ourselves.

Build main and this branch with the same toolchain:

zig build -Dvulkan=true vulkan-host-test stage3-host-test qwen35-model-host-test
zig build -Dvulkan=true stage3-gpu-build vulkan-model-probe-build native-vulkan
  1. Attention contract, on this branch: zig-out/bin/tf-attention-contract --vendor VENDOR_ID --timeout-ms 600000.
    Every case must pass, including the 7169/8192 cases, the 16 sdpa-served lines and all 3 controls CAUGHT.
  2. Tokens, pinned Qwen3.5-2B, same device, fresh state, one stream:
    zig-out/bin/tf-qwen35-model --vendor VENDOR_ID --context N MODEL INPUT_IDS.npy ORACLE OUT
    • 4096 and 7168 keys: the returned token IDs and logits_bf16.bin must be byte-equal (cmp) between main and
      this branch.
    • 8192 keys: main refuses this length, so there is no main baseline. Compare this branch against commit 1 alone
      (global scores, no tile). IDs and logits_bf16.bin must be byte-equal. Please also confirm main refuses 8192.
  3. BF16 prefill rows 2..64: the probe cuts a whole prompt into 64-row chunks, so a prompt of L tokens ends with a
    chunk of L - 64 rows for L in 66..128. Run every prompt length 66..128 (final chunks of 2..64 rows) on main and
    this branch. IDs and logits_bf16.bin must be byte-equal for each length.
  4. Timing, after the bytes match: whole-prompt prefill at 254 and about 6173 tokens, main vs this branch,
    alternating runs with the same settings. Please also note the peak memory at --context 8192.

Please post the GPU model, driver, commands and outputs, including any failure or timeout.

ashhart and others added 2 commits December 5, 2026 09:12
Refs #3417. Raise the native Vulkan SDPA limit from 7168 to 8192 visible keys. Shared score storage keeps the
7168-key cap. Above it, a GLOBAL_SCORES specialization of the existing decode shader stores F32 scores in a
per-row, per-head region of an engine-owned score view. A request above 7168 without that view fails before
dispatch. 8193 is refused, never clamped.

The arena is 16 MiB for capacity 1..7168 and 32 MiB for 7169..8192. Score arithmetic, barriers and rounding are
unchanged; only the score storage location differs.

Host tests cover attendPlan at 7169/8192/8193, the score workspace size and overflow refusals, arena selection and
budget, and request/CLI bounds. The attention contract adds BF16 and FP32 cases at 7169 and 8192 and a chunk that
crosses 7168.

Thanks @joshuaswarren for the capacity design and contract cases.

Co-authored-by: Joshua Warren <816217+joshuaswarren@users.noreply.github.com>
Refs #3431. BF16 query chunks of 2..64 rows use a new three-stage prefill tile: scores, softmax and PV. Four rows
and four GQA query heads share each K and V tile load. All FP32 queries, one-row decode and larger chunks keep the
existing fused route.

The tile keeps main's arithmetic: scale times Q before an ascending dot, the 256-lane max and sum trees, 16-key PV
partials and one divide before RNE BF16. Each row's products are guarded by its own causal bound, so a future
NaN or Inf value never enters an earlier row.

Tile workspace is sized from the admitted capacity: 16 MiB plus 4 KiB at 8192. The attention contract adds 16
served-shape checks (rows 64/64/64/62 at prefixes 0/64/128/192, BF16 and native FP32, finite and future NaN/Inf)
and three planted controls that must fail.

Thanks @joshuaswarren for the tiling design and the served-shape regression.

Co-authored-by: Joshua Warren <816217+joshuaswarren@users.noreply.github.com>

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant